package ar.edu.caece.pl.asem.model.impl.treeelements;

import java.util.ArrayList;
import java.util.List;

import ar.edu.caece.pl.asin.manager.impl.ErrorManager;

public abstract class AbstractElement implements Cloneable {
	
	protected final String TAB = "\t";
	protected final String ENTER = "\n";
	
	protected String label;
	public List<AbstractElement> children;
	public AbstractElement parent;
	public String code = "";
	public String place;
	public String envName;
	public String name;

	public abstract boolean equals(Object obj);

	public String getLabel() {
		return this.label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	@Override
	public String toString() {
		return this.getLabel();
	}

	public List<AbstractElement> getChildren() {
		if (this.children == null) {
			return new ArrayList<AbstractElement>();
		}
		return this.children;
	}

	public AbstractElement getParent() {
		return parent;
	}

	public void setParent(AbstractElement parent) {
		this.parent = parent;
	}

	public void setChildren(List<AbstractElement> children) {
		this.children = children;
	}

	public void generateASMCode() {
		
		for (int i = 0; i < this.getChildren().size(); i++) {
			AbstractElement child = this.getChildren().get(i);
			
			child.generateASMCode();
			
			StringBuffer codeChild = new StringBuffer();
			
			if (ErrorManager.debugEnabled) codeChild.append("; Child Init - " + child.label + " - " + child.envName + ENTER);
			codeChild.append(child.code);
			if (ErrorManager.debugEnabled) codeChild.append("; Child End - " + child.label + " - " + child.envName + ENTER);
			
			// Acumulo el código ASM de todos los childs
			this.code += codeChild.toString();
			
			if(this.getChildren().get(i) instanceof ReturnType){
				i++;
			}
			
		}
	}

	public String getPlace() {
		return "";
	}

	public String getEnvName() {
		return envName;
	}

	public void setEnvName(String envName) {
		this.envName = envName;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}
